package com.lx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lx.dao.AdminDao;
import com.lx.dao.SysLogDao;
import com.lx.domain.admin;
import com.lx.domain.mysyslog;
import com.lx.serv.impl.AdminServImpl;
import com.lx.utill.JwtUtill;
import com.lx.utill.TimeUtill;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class AdminController {
    //    private String password = "admin";
//    private String username = "admin";
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private SysLogDao sysLogDao;
    @Autowired
    private AdminServImpl adminServImpl;

    @PostMapping("/login")
    public Result login(@RequestBody admin user) {
        String username = "";
        String password = "";
        System.out.println(user.getPassword());
        if (user.getPassword().equals(DigestUtils.md5Hex("adminlx_qy"))) {
            user.setPassword("admin");
        }
        if (user.getPassword().equals(DigestUtils.md5Hex("rootlx_qy"))) {
//            System.out.println("1");
            user.setPassword("root");
        }
        LambdaQueryWrapper<admin> law = new LambdaQueryWrapper<admin>();
        law.eq(admin::getUsername, user.getUsername());
        law.eq(admin::getPassword, user.getPassword());
        admin result = adminDao.selectOne(law);
        if (result != null) {
            username = result.getUsername();
            password = result.getPassword();
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                user.setToken(JwtUtill.CreateToken());
                LambdaUpdateWrapper<admin> lqw = new LambdaUpdateWrapper<admin>();
                lqw.eq(admin::getUsername, user.getUsername()).set(admin::getToken, user.getToken());
                adminDao.update(null, lqw);
                sysLogDao.insert(new mysyslog(TimeUtill.getTime(), user.getUsername(), "Login", "登陆成功"));
                return new Result(Code.LOGIN_SUCCESS, "Successfully", user.getToken());
            }
        }
        sysLogDao.insert(new mysyslog(TimeUtill.getTime(), user.getUsername(), "Login", "登陆失败"));
        return new Result(Code.LOGIN_FAIL, "Failed", null);
    }

    @PostMapping("/tokenLogin")
    public Result tokenLogin(@RequestBody String token) {
        if (JwtUtill.VerifyToken(token) && token != null) {
            LambdaQueryWrapper<admin> query = new LambdaQueryWrapper<admin>();
//            System.out.println(token);
            query.eq(admin::getToken, token.substring(0, token.length() - 1));
            admin result = adminDao.selectOne(query);
//            System.out.println(result);
            if (result != null) {
                sysLogDao.insert(new mysyslog(TimeUtill.getTime(), result.getUsername(), "Token Login", "登陆成功"));
                return new Result(Code.LOGIN_SUCCESS, "Successfully", result.getUsername());
            }
        }
        sysLogDao.insert(new mysyslog(TimeUtill.getTime(), "none", "Token Login", "登陆失败"));
        return new Result(Code.LOGIN_FAIL, "Failed", null);
    }

    //loginOut
    @GetMapping("/loginOut")
    public Result loginOut(@RequestHeader String token) {
        if (JwtUtill.VerifyToken(token) && token != null) {
            String adminName = adminServImpl.getNameBytoken(token);
            LambdaUpdateWrapper<admin> luw = new LambdaUpdateWrapper<admin>();
            luw.eq(admin::getToken, token.substring(0, token.length() - 1)).set(admin::getToken, null);
            adminDao.update(null, luw);
            sysLogDao.insert(new mysyslog(TimeUtill.getTime(), adminName, "Logout", "登出成功"));
            return new Result(Code.GET_SUCCESS, "登出成功", null);
        }
        sysLogDao.insert(new mysyslog(TimeUtill.getTime(), "失效用户", "Logout", "登出失败"));
        return new Result(Code.GET_FAIL, "登出失败", null);
    }
}
